<?php
	if (!current_user_can('manage_options'))
	{
		wp_die('You do not have sufficient permissions to access this page.');
	}
    $para = $_GET;
    $para['page'] = 'following-emails';
    $para['action'] = 'delete_email';
    
    if($_GET['action'] = 'delete_email' && !empty($_GET['flid']))
    {
        global $wpdb;
        $follow_id = $_GET['flid'];
        $tb_follow = $wpdb->prefix . "follow";
        $qr = "DELETE FROM $tb_follow WHERE follow_id = $follow_id";
        if($wpdb->query($qr) == 1)
            echo "<div id='message' class='updated'>Deleted</div>";            
    }
        
    global $wpdb;
    $qr = "select follow_id,follow_what,user_email,value,time,user_ip,STATUS from {$wpdb->follow} order by time desc";
    $rs = $wpdb->get_results($qr);
?>
<style type="text/css">
    table.ex{border: 1px solid;text-align: left;}
    table.ex tr td{border: 1px solid #BBB;}
    table.ex #site{background: whitesmoke;}
    .em_head{font-weight: bold;}
</style>
      <div class="wrap">
        <?php screen_icon('themes'); ?> <h2>Following Emails ( <?php echo count($rs); ?> )</h2>
        <form method="POST" action="">  
            <table class="form-table ex">
                <tr valign="top" class="em_head">
                    <td><label>Follow</label></td>
                    <td><label>Emails</label></td>
                    <td><label>Store Name</label></td>
                    <td><label>Time (GMT +7)</label></td>
                    <td><label>IP</label></td>
                    <td><label>Status</label></td>
                    <td><label>Action</label></td>
                </tr>
                <?php if(count($rs) > 0): foreach ($rs as $obj): ?>
                <tr <?php if($obj->follow_what == 'site') echo "id='site'"; ?>>
                    <td><?php echo strtoupper($obj->follow_what); ?></td>
                    <td><?php echo $obj->user_email; ?></td>
                    <td><a href="<?php echo get_permalink($obj->value); ?>" target="_blank"><?php echo get_post_field('post_title', $obj->value); ?></a></td>
                    <td><?php echo $obj->time; ?></td>
                    <td><?php echo $obj->user_ip.'( '.iptocountry($obj->user_ip).' )'; ?></td>
                    <td><?php echo ($obj->STATUS == 1?'Unsubscribed':''); ?></td>
                    <?php $para['flid'] = $obj->follow_id; ?>
                    <td><a href="<?php echo admin_url('admin.php').'?'.http_build_query($para); ?>" onclick="return confirm('Delete this email?');">delete</a></td>
                </tr>
                <?php endforeach;endif; ?>
            </table>
        </form>  
      </div>
<?php
    function iptocountry($ip)
	{
        if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) != false)
            return 'IPV6';
        if($ip == '::1' || filter_var($ip, FILTER_VALIDATE_IP) == false)
            return 'N/A';
       
		$numbers = preg_split("/\./", $ip);
		include ("ip_files/" . $numbers[0] . ".php");
		$code = ($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
		foreach ($ranges as $key => $value)
		{
			if ($key <= $code)
			{
				if ($ranges[$key][0] >= $code)
				{
					$two_letter_country_code = $ranges[$key][1];
					break;
				}
			}
		}
		if ($two_letter_country_code == "")
		{
			$two_letter_country_code = "unknown";
		}
		include ("ip_files/countries.php");
    	$country_name = $countries[$two_letter_country_code][1];
        return $country_name;
	}

?>  